package com.tarena.lbs.business.servcie.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tarena.lbs.base.protocol.pager.PageResult;
import com.tarena.lbs.business.dao.repository.StoreRepository;
import com.tarena.lbs.business.pojo.param.StoreParam;
import com.tarena.lbs.business.pojo.po.StorePO;
import com.tarena.lbs.business.pojo.query.AreaStoreQuery;
import com.tarena.lbs.business.pojo.query.AreaStoreQuery1;
import com.tarena.lbs.business.pojo.query.StoreQuery;
import com.tarena.lbs.business.pojo.vo.StoreVO;
import com.tarena.lbs.business.servcie.StoreService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class StoreServiceImpl implements StoreService {
    @Autowired
    private StoreRepository storeRepository;
    @Override
    public PageResult<StoreVO> selectStoreList(StoreQuery query) {
        log.info("查询店铺列表{}", query);
        PageResult<StoreVO> result=new PageResult<>( query.getPageSize(), query.getPageNo());
        PageInfo<StorePO> pageInfo=getPageBusiness( query);
        result.setTotal(pageInfo.getTotal());
        List<StorePO> pos=pageInfo.getList();
        List<StoreVO> vos=new ArrayList<>();
        if (pos!=null && pos.size()>0){
            vos=pos.stream().map(po->{
                StoreVO vo=new StoreVO();
                BeanUtils.copyProperties(po,vo);
                return vo;
            }).collect(Collectors.toList());
        }
        result.setObjects(vos);
        return result;
    }
    private PageInfo<StorePO> getPageBusiness(StoreQuery query) {
        PageHelper.startPage(query.getPageNo(), query.getPageSize());
        log.info("查询店铺列表页数{}，{}", query.getPageNo(), query.getPageSize());
        log.info("查询店铺列表参数{}", query);
        List<StorePO> pos=storeRepository.list(query);
        return new PageInfo<>(pos);
    }

    @Override
    public void addStore(StoreParam param) {
        log.debug("新增店铺{}", param);
        StorePO po=new StorePO();
        BeanUtils.copyProperties(param,po);
        po.setStoreStatus(0);
        po.setCreateTime(new Date());
        storeRepository.addStore(po);
    }

    @Override
    public List<StoreVO> selectStoreByCity(AreaStoreQuery1 query) {
        log.info("查询区域下的店铺列表{}", query);
        List<StoreVO> pos=storeRepository.listById(query);
        return pos;
    }

    @Override
    public void delete(Integer id) {
        storeRepository.deleteStore(id);
    }

    @Override
    public void updateStatus(Integer id, Integer status) {
        StorePO store=new StorePO();
        store.setId(id);
        store.setStoreStatus(status);
        store.setUpdateTime(new Date());
        storeRepository.updateStore(store);
    }


}